
package biblio.service.author;


import biblio.bean.AuthorBean;
import biblio.dao.AuthorDAO;
import biblio.domain.Author;
import biblio.exception.NoSuchEntityException;
import biblio.util.SourceName;
import biblio.util.interfaces.RequestOperationHelper;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;


/**
 *
 * @author mephisto
 */
@ManagedBean(name="authorService")
@ApplicationScoped
public class AuthorServiceImpl implements AuthorService
{
    public void create()
    {
        RequestOperationHelper rOH = (RequestOperationHelper)appContext.getBean("rOH");
        authorBean = (AuthorBean) rOH.getBean("authorBean");
        Author author = new Author();
        author.setFirstName(authorBean.getFirstName());
        author.setLastName(authorBean.getLastName());

        AuthorDAO authorDAO = (AuthorDAO)appContext.getBean("authorDAO");
        authorDAO.create(author);
        authorBean.clear();
    }

     public void update(Author author)
    {
        log.debug("Author for update: " + author.getId());

        AuthorDAO authorDAO = (AuthorDAO)appContext.getBean("authorDAO");
        try
        {
            authorDAO.update(author);
        }
        catch (NoSuchEntityException ex)
        {
            log.error("Entity with id: " + author.getId() + "does not exist in the database");
            log.error(ex.getMessage());
        }
    }

    public void delete(Author author)
    {
        AuthorDAO authorDAO = (AuthorDAO)appContext.getBean("authorDAO");
        try
        {
            authorDAO.delete(author);
        }
        catch(DataAccessException e)
        {
            log.error("Entity with id: " + author.getId() + "does not exist in the database");
            log.error(e);
        }
    }

    static
    {
        appContext = new ClassPathXmlApplicationContext( new String[] {
                SourceName.AUX_APPLICATION_CONTEXT,
                SourceName.DAO_APPLICATION_CONTEXT
        });
    }


    private static ApplicationContext appContext;
    private static Logger log = Logger.getLogger(AuthorServiceImpl.class);
    private AuthorBean authorBean;
}
